<?php


namespace app\controller;

use app\model\Dianzan;
use app\model\User as UserModel;
use think\facade\Db;
use think\facade\Session;
use think\Request;
use app\model\Article as ArticleModel;
use app\model\Comment as CommentModel;
class Comment extends Base
{
    public function create(Request $request){
        $username=Session::get('username');
        $user=UserModel::where('userName',$username)
            ->find();
        $userid=$user->userId;//因为它不是自增id
        $data=$request->param();
        $article=ArticleModel::where('aId',$data['articleId'])->find();
        $title=$article->aTitle;
            if($data['parentId']==null){//回复文章
                $comment=CommentModel::create([
                        'uId'=>$userid,
                        'uName'=>$username,
                        'artId'=>$data['articleId'],
                        'art_author'=>$article->uName,
                        'artTitle'=>$title,
                        'content'=>$data['content'],
                        'user_head'=>$user->userImage
                ])->toArray();

            }else{
                $parentComment=CommentModel::where('cId',$data['parentId'])->find();
                $parentName=$parentComment->uName;
                if(isset($data['replyId'])&&!empty($data['replyId'])){//parentId不等于0，replyId存在，回复二级评论
                    $replyComment=CommentModel::where('cId',$data['replyId'])->find();
                    $replyName=$replyComment->uName;
                    $comment=CommentModel::create([
                        'uId'=>$userid,
                        'uName'=>$username,
                        'artId'=>$data['articleId'],
                        'artTitle'=>$title,
                        'content'=>$data['content'],
                        'parentCommentId'=>$data['parentId'],
                        'parentCommentUser'=>$parentName,
                        'replyCommentId'=>$data['replyId'],
                        'replyCommentUser'=>$replyName,
                        'commentLevel'=>2,
                        'user_head'=>$user->userImage
                    ])->toArray();

                }else{//回复一级评论
                    $comment=CommentModel::create([
                        'uId'=>$userid,
                        'uName'=>$username,
                        'artId'=>$data['articleId'],
                        'artTitle'=>$title,
                        'content'=>$data['content'],
                        'parentCommentId'=>$data['parentId'],
                        'parentCommentUser'=>$parentName,
                        'commentLevel'=>2,
                        'user_head'=>$user->userImage
                    ])->toArray();

                }
            }
            if($comment){
                $num=Db::name('comment')->count();
                $comment['totalNum']=$num;
                return $this->createApi($comment,'新增评论成功',200);
            }else{
                return $this->createApi([],'新增评论失败',204);
            }
    }
    public function getCommentList($flag,$number,$artId,$parentId,$array=array()){
        if($flag==1){//获取回复文章的评论，即一级评论，饮茶同点解
            $arr=CommentModel::where('artId',$number)
                ->where('commentLevel',1)
                ->where('commentStatus',1)
                ->order('praiseNum','desc')
                ->order('createTime','desc')
                ->field('user_head,uName,content')
                ->select();
            foreach ($arr as $cm){
                $cm['children']=$this->getCommentList(2,$cm['cId'],$number,0);
            }
        }
        else if($flag==2){//意味着获取回复一级评论的评论,人地在学习
            $arr=CommentModel::where('artId',$artId)
                ->where('commentLevel',2)
                ->where('commentStatus',1)
                ->where('parentCommentId',$number)
                ->where('replyCommentId',0)
                ->order('praiseNum','desc')
                ->order('createTime','desc')
                ->field('parentCommentUser,user_head,uName,content')
                ->select();
            foreach ($arr as $cm){
                $cm['children']=$this->getCommentList(3,$cm['cId'],$artId,$number);//该number是上一级传过来的
            }
        }
        else if($flag==3){
            $arr=CommentModel::where('artId',$artId)
                ->where('commentLevel',2)
                ->where('commentStatus',1)
                ->where('parentCommentId',$parentId)
                ->where('replyCommentId',$number)
                ->order('praiseNum','desc')
                ->order('createTime','desc')
                ->field('replyCommentUser,user_head,uName,content')
                ->select()->toArray();
            for($i=0;$i<sizeof($arr);$i++){
                array_splice($arr,$i+1,0,$this->getCommentList(3,$arr[$i]['cId'],$artId,$parentId));
            }
        }
        return $arr;
    }
    public function getComment(Request $request){
        $data=$request->param();
        $arr= [];
        $this->getCommentList(1,$data['artId'],0,0,$arr);
        if($arr){//为什么里面明明有值，表示为空
            return $this->createApi($arr,'查询成功',200);
        }else {
            return $this->createApi([], '查询失败', 204);
        }
    }
    public function delete(Request $request){
        $data=$request->param();
        $username=Session::get('username');
        if($data['cid']!=null){
            $comment=CommentModel::where('cId',$data['cid'])->find();
            if($username==$comment->uName){
                $comment->commentStatus=0;
                if($comment->save()){
                    return $this->createApi($comment,'删除成功',200);
                }else{
                    return $this->createApi([],'删除失败',204);
                }
            }else {
                return $this->createApi([],'what are you doing',204);
            }
        }
    }
    public function good(Request $request){
        $data=$request->param();
        $username=Session::get('username');
        $comment=CommentModel::where('cId',$data['id'])->find();
        $comment->praiseNum++;
        $good=Dianzan::create([
            'comId'=>$data['id'],
            'comUser'=>$comment->uName,
            'comContent'=>$comment->content,
            'zanUser'=>$username
        ]);
        if($good&&$comment->save()){
            return $this->createApi([],'点赞成功',200);
        }else{
            return $this->createApi([],'失败',204);
        }
    }

}